package com.yuandian.customer.perception.service.impl;

import com.yuandian.api.customer_perception.po.TopBaseParams;
import com.yuandian.api.customer_perception.vo.AvgTakeTimeTopNVo;
import com.yuandian.api.customer_perception.vo.TopAnalysisOverviewVo;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.customer.perception.mapper.BusiTopAnalysisMapper;
import com.yuandian.customer.perception.service.IBusiTopAnalysisService;
import com.yuandian.enums.TableNameEnum;
import com.yuandian.po.MultipleTableInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 类说明: 全业务TOP 分析服务实现类
 *
 * @Date 2021/8/19 9:54
 * @Author User
 */
@Service
public class BusiTopAnalysisServiceImpl implements IBusiTopAnalysisService {

    @Resource
    private BusiTopAnalysisMapper busiTopAnalysisMapper;


    @Override
    public TopAnalysisOverviewVo queryTopAnalysisOverview(TopBaseParams params) {
        // 1、获取表信息集
        MultipleTableInfo info = new MultipleTableInfo();
        info.setTableName(TableNameEnum.KHGZ_BUSI_SYS_DETAIL_STAT.getName());
        // 2、校验表是否存在，剔除不存在的表
        List<MultipleTableInfo> multipleTableInfoList = Collections.singletonList(info);
        // 3、设置表信息集
        params.setMultipleTableInfoList(multipleTableInfoList);
        // 查询受理量
        List<TopAnalysisOverviewVo> complete = busiTopAnalysisMapper.queryTopComplete(params);
        // 查询失败量
        List<TopAnalysisOverviewVo> fail = busiTopAnalysisMapper.queryTopFail(params);
        // 查询操作耗时
        List<TopAnalysisOverviewVo> visitTime = busiTopAnalysisMapper.queryTopAvgVisitTime(params);
        // 查询响应耗时
        List<TopAnalysisOverviewVo> dealTime = busiTopAnalysisMapper.queryTopAvgDealTime(params);

        return TopAnalysisOverviewVo.build(complete, fail, visitTime, null, dealTime);
    }


    @Override
    public List<TopAnalysisOverviewVo> queryCompleteTopN(TopBaseParams params) {
        // 1、获取表信息集
        MultipleTableInfo info = new MultipleTableInfo();
        info.setTableName(TableNameEnum.KHGZ_BUSI_SYS_DETAIL_STAT.getName());
        // 2、校验表是否存在，剔除不存在的表
        List<MultipleTableInfo> multipleTableInfoList = Collections.singletonList(info);
        // 3、设置表信息集
        params.setMultipleTableInfoList(multipleTableInfoList);
        // 查询当前
        List<TopAnalysisOverviewVo> complete = busiTopAnalysisMapper.queryTopComplete(params);

        List<String> businessCodes = new ArrayList<>();
        complete.forEach(item -> businessCodes.add(item.getCompleteCode()));

        // 查询历史峰值和谷值
        List<TopAnalysisOverviewVo> historyComplete = new ArrayList<>();
        if (businessCodes.size() > 0) {
            List<TopAnalysisOverviewVo> tmp = busiTopAnalysisMapper.queryHistoryCompleteTop(businessCodes);
            if (tmp.size() > 0) {
                historyComplete.addAll(tmp);
            }
        }

        Map<String, TopAnalysisOverviewVo> temp = new HashMap<>();
        historyComplete.forEach(item -> temp.put(item.getCompleteCode(), item));

        complete.forEach(item -> {
            TopAnalysisOverviewVo hisVo = temp.get(item.getCompleteCode());
            if (null != hisVo) {
                item.setMinCompleteCount(hisVo.getMinCompleteCount());
                item.setMaxCompleteCount(hisVo.getMaxCompleteCount());
            }
        });


        complete.forEach(TopAnalysisOverviewVo::translateBusiness);
        return complete;
    }

    @Override
    public List<TopAnalysisOverviewVo> queryFailTopN(TopBaseParams params) {
        // 1、获取表信息集
        MultipleTableInfo info = new MultipleTableInfo();
        info.setTableName(TableNameEnum.KHGZ_BUSI_SYS_DETAIL_STAT.getName());
        // 2、校验表是否存在，剔除不存在的表
        List<MultipleTableInfo> multipleTableInfoList = Collections.singletonList(info);
        // 3、设置表信息集
        params.setMultipleTableInfoList(multipleTableInfoList);

        // 查询当前
        List<TopAnalysisOverviewVo> fail = busiTopAnalysisMapper.queryTopFail(params);

        List<String> businessCodes = new ArrayList<>();
        fail.forEach(item -> businessCodes.add(item.getFailCode()));

        // 查询历史峰值和谷值
        List<TopAnalysisOverviewVo> historyFail = new ArrayList<>();
        if (businessCodes.size() > 0) {
            List<TopAnalysisOverviewVo> tmp = busiTopAnalysisMapper.queryHistoryFailTop(businessCodes);
            if (tmp.size() > 0) {
                historyFail.addAll(tmp);
            }
        }

        Map<String, TopAnalysisOverviewVo> temp = new HashMap<>();
        historyFail.forEach(item -> temp.put(item.getFailCode(), item));

        fail.forEach(item -> {
            TopAnalysisOverviewVo hisVo = temp.get(item.getFailCode());
            if (null != hisVo) {
                item.setMinFailCount(hisVo.getMinFailCount());
                item.setMaxFailCount(hisVo.getMaxFailCount());
            }
        });


        fail.forEach(TopAnalysisOverviewVo::translateBusiness);
        return fail;
    }


    @Override
    public AvgTakeTimeTopNVo queryAvgVisitTimeTopN(TopBaseParams params) {
        // 1、获取表信息集
        MultipleTableInfo info = new MultipleTableInfo();
        info.setTableName(TableNameEnum.KHGZ_BUSI_SYS_DETAIL_STAT.getName());
        // 2、校验表是否存在，剔除不存在的表
        List<MultipleTableInfo> multipleTableInfoList = Collections.singletonList(info);
        // 3、设置表信息集
        params.setMultipleTableInfoList(multipleTableInfoList);

        // 升序
        params.setOrderBy("asc");
        List<TopAnalysisOverviewVo> ascVisitTime = busiTopAnalysisMapper.queryTopAvgVisitTime(params);
        ascVisitTime.forEach(TopAnalysisOverviewVo::translateBusiness);

        // 降序
        params.setOrderBy("desc");
        List<TopAnalysisOverviewVo> descVisitTime = busiTopAnalysisMapper.queryTopAvgVisitTime(params);
        descVisitTime.forEach(TopAnalysisOverviewVo::translateBusiness);

        return AvgTakeTimeTopNVo.build(ascVisitTime, descVisitTime);
    }

    @Override
    public AvgTakeTimeTopNVo queryAvgDealTimeTopN(TopBaseParams params) {
        // 1、获取表信息集
        MultipleTableInfo info = new MultipleTableInfo();
        info.setTableName(TableNameEnum.KHGZ_BUSI_SYS_DETAIL_STAT.getName());
        // 2、校验表是否存在，剔除不存在的表
        List<MultipleTableInfo> multipleTableInfoList = Collections.singletonList(info);
        // 3、设置表信息集
        params.setMultipleTableInfoList(multipleTableInfoList);

        // 升序
        params.setOrderBy("asc");
        List<TopAnalysisOverviewVo> ascResponseTime = busiTopAnalysisMapper.queryTopAvgDealTime(params);
        ascResponseTime.forEach(TopAnalysisOverviewVo::translateBusiness);

        // 降序
        params.setOrderBy("desc");
        List<TopAnalysisOverviewVo> descResponseTime = busiTopAnalysisMapper.queryTopAvgDealTime(params);
        descResponseTime.forEach(TopAnalysisOverviewVo::translateBusiness);

        return AvgTakeTimeTopNVo.build(ascResponseTime, descResponseTime);
    }
}
